package com.ad.pet.app.mall.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ad.pet.R;
import com.ad.pet.models.remote.mall.SkuProps;

import java.util.HashMap;
import java.util.List;

/**
 * Created by SEELE on 2019/5/14.
 */

public class SkuAdapter extends BaseQuickAdapter<SkuProps, BaseViewHolder> {
    private OnSkuItemClickLinstenr mOnSkuItemClickLinstenr;
//    private final SpaceItemDecoration mSpaceItemDecoration;
//    boolean flag = true;
    public SkuAdapter(@Nullable List<SkuProps> data) {
        super(R.layout.item_sku_select_layout, data);
//        mSpaceItemDecoration = new SpaceItemDecoration(DensityUtil.dip2px(5));
    }

    @Override
    protected void convert(final BaseViewHolder helper, SkuProps item) {
        final TextView mTvSkuKey = helper.getView(R.id.tv_sku_key);
        RecyclerView mRecyclerAttr = helper.getView(R.id.recycler_sku_item);
        mTvSkuKey.setText(item.getProp());

//        GridLayoutManager flowLayoutManager = new GridLayoutManager(mContext,4)
        com.library.flowlayout.FlowLayoutManager flowLayoutManager = new com.library.flowlayout.FlowLayoutManager();
//       if (flag) {
//           mRecyclerAttr.addItemDecoration(mSpaceItemDecoration);
//           flag = false;
//       }

        mRecyclerAttr.setLayoutManager(flowLayoutManager);

        final SkuItemAdapter skuItemAdapter = new SkuItemAdapter(item.getValue());
        mRecyclerAttr.setAdapter(skuItemAdapter);

        skuItemAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mOnSkuItemClickLinstenr != null) {
                    mOnSkuItemClickLinstenr.onSkuItemClick(helper.getLayoutPosition(), position);
                }
                skuItemAdapter.setChoseItem(position);
                skuItemAdapter.notifyDataSetChanged();

            }
        });


    }

    public HashMap<String, String> getMap() {
        HashMap<String, String> map = new HashMap<>();
        for (int i = 0; i < mData.size(); i++) {
            for (int j = 0; j < mData.get(i).getValue().size(); j++) {
                if (mData.get(i).getValue().get(j).isChose()) {
                    map.put(mData.get(i).getProp(), mData.get(i).getValue().get(j).getName());
                }
            }
        }
        return map;
    }

    public int getUnselectPosition() {

        for (int i = 0; i < mData.size(); i++) {
            int countUnChose = 0;
            for (int j = 0; j < mData.get(i).getValue().size(); j++) {
                if (mData.get(i).getValue().get(j).isChose()) {
                    countUnChose++;
                }
            }
            if (countUnChose == 0) {
                return i;
            }
        }
        return -1;
    }

    public interface OnSkuItemClickLinstenr {
        void onSkuItemClick(int positon, int itemPosition);
    }

    public void setOnSkuItemClickLinstenr(OnSkuItemClickLinstenr onSkuItemClickLinstenr) {
        mOnSkuItemClickLinstenr = onSkuItemClickLinstenr;
    }
}
